//
//  SongsListTableViewController.m
//  WXMusic
//
//  Created by 漫步人生路 on 15/9/2.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "SongsListTableViewController.h"
#import "SongsListTableViewCell.h"
#import "SongsListModel.h"
#import "SongsHeaderView.h"
#import "PlayViewController.h"
@interface SongsListTableViewController ()
@property (nonatomic, strong) NSMutableArray *songsArray;
@property (nonatomic, strong) UIImageView *songsHeader;
@end

@implementation SongsListTableViewController

- (NSMutableArray *)songsArray
{
    if (!_songsArray) {
        _songsArray = [[NSMutableArray alloc]init];
    }
    return _songsArray;
}
//数据解析
- (void)reloadSongsList
{
    if ([self.songsType isEqualToString:@"首页"]) {
        NSString *url = [[kSongsListOne stringByAppendingString:self.songsListCode] stringByAppendingString:kSongsListTwo];
        [LORequestManger GET:url success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            [self.songsHeader sd_setImageWithURL:[NSURL URLWithString:dic[@"pic_w700"]]];
            for (NSDictionary *content in dic[@"content"]) {
                SongsListModel *songsList = [SongsListModel songsModelWithDictionary:content];
                [self.songsArray addObject:songsList];
            }
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }else if ([self.songsType isEqualToString:@"排行榜"])
    {
        NSString *url = [[kSongTop stringByAppendingString:self.songsListCode] stringByAppendingString:kTopList];
        [LORequestManger GET:url success:^(id response) {
            NSDictionary *dic = (NSDictionary *)response;
            NSDictionary *dict = [dic objectForKey:@"billboard"];
            [self.songsHeader sd_setImageWithURL:[NSURL URLWithString:[dict objectForKey:@"pic_s640"]]];
            for (NSDictionary *songInfo in [dic objectForKey:@"song_list"]) {
                SongsListModel *songModel = [SongsListModel songsModelWithDictionary:songInfo];
                [self.songsArray addObject:songModel];
            }
            [self.tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.songsHeader = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 270)];
    self.tableView.tableHeaderView = self.songsHeader;
    [self reloadSongsList];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundView = kBlurImageView;
    [self.tableView registerClass:[SongsListTableViewCell class] forCellReuseIdentifier:@"songsList"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.songsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SongsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songsList" forIndexPath:indexPath];
    SongsListModel *songsListModle = [[SongsListModel alloc]init];
    songsListModle = [self.songsArray objectAtIndex:indexPath.row];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    [cell setValueWithModel:songsListModle];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   SongsListModel *songListModel = [[SongsListModel alloc]init];
    PlayViewController *playView = [[PlayViewController alloc]init];

    for (songListModel in self.songsArray)
    {
        NSString *url = songListModel.song_id;
        [[LouMusicPlayer shareMusicPlayer].modelArray addObject:url];
    }
    //获取下标
    [LouMusicPlayer shareMusicPlayer].index = indexPath.row;
    
    [self presentViewController:playView animated:YES completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
